<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <base href="<%=request.getContextPath()%>/"/>
    <script type="text/javascript" src="static/js/jquery-2.1.1.min.js"></script>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <script src="static/js/jquery-2.1.1.min.js"></script>
    <%@include file="../layUI.jsp"%>


</head>
<body>
<form action="page/smallclass/smallAdd.jsp" method="post">
    <input type="submit" value="添加小分类" class="layui-btn">
</form>
<form action="doSmall?action=smallQueryLike&pageNumber=1" method="post" >
    <label>小分类名</label>
    <input type="text" name="smallName" id="smallName">
    <label>大分类名</label>
    <select name="bigName" id="bigName">
      <option value="-1">全部</option>
    </select>
    <input class="layui-btn" type="button" value="查询" onclick="SelSmall(1)">
</form>
        <table width="100%" style="text-align: center" class="layui-table">
            <tr>
                <th>小分类ID</th>
                <th>小分类名称</th>
                <th>大分类名称</th>
                <th>小分类描述</th>
                <th colspan="2">操作</th>
            </tr>

            <tbody id="theBody">

            </tbody>
            <tr>
                <td colspan="5">
                    <%@include file="../Page.jsp"%>
                </td>
            </tr>
        </table>

<h3></h3>
<script type="text/javascript">
    var currentPage =1;
    //显示小分类数据
    function SelSmall(currentPage) {
        var smallName = $("#smallName").val();
        var bigName = $("#bigName").val();
        $.getJSON("/doSmall",{"action":"smallQueryLike","smallName":smallName,"bigName":bigName,"currentPage":currentPage},callback)
        function callback(data) {
            $("#theBody").html("");
            if(data.list.length<=0){
                $("h3").html("没有小分类");
                $("table").hide();
            }else{
                $("table").show();
                $("h3").html("");
            }
            for (var i = 0; i < data.list.length; i++) {
                $("#theBody").append("<tr>" +
                    "<td>"+data.list[i].smallId+"</td>" +
                    "<td>"+data.list[i].smallName+"</td>" +
                    "<td>"+data.list[i].bigClass.bigName+"</td>" +
                    "<td>"+data.list[i].smallText+"</td>" +
                    "<td>" +
                    "<a href='/page/smallclass/SmallUpdate.jsp?id="+data.list[i].smallId+"'>编辑</a>" +
                    "&nbsp; &nbsp;&nbsp;" +
                    "<a href='javascript:void(0)' onclick='delSsms("+data.list[i].smallId+",this)'>删除</a>" +
                    "</td>" +
                    "</tr>")
            }
            $("#total").html(data.totalCount);
            $("#totalPages").html(data.totalPages);
            $("#pageNo").html(currentPage);
            $("#jump1").html("");
            var iii = 1;
            var flag = 0;
            for (var i = 1; i <= parseInt(data.totalPages); i++){
                var ii = parseInt($("#pageNo").html() / 10) ; // 起始页
                i = ii * 10;
                if (i % 10 == 0 && ii >= 1 && flag == 0){
                    $("#jump1").append("<option name=\"op1\" value=\"" + i+ "\">" + i + "页</option>");
                    flag = 1;
                }
                i += iii++;
                $("#jump1").append("<option name=\"op1\" value=\"" + i+ "\">" + i + "页</option>")
                if(i % 10 == 0 ){
                    break;
                }
            }
            totalPags =  $("#totalPages").html();
            if (flag == 0) {
                document.getElementsByName("op1")[(currentPage - 1) % 10].selected=true;
            } else {
                document.getElementsByName("op1")[(currentPage) % 10].selected=true;
            }
        }
    }
    //初始化数据
    $(function () {
       //    显示大分类下拉列表
        $.getJSON("/doSmall",{"action":"selSmallBigName"},callback);
        function callback(data) {
            $(data).each(function (i) {
                $("#bigName").append(" <option value='"+(i+1)+"'>"+this.bigName+"</option>");
            })
        }

        SelSmall(currentPage);
        //首页
        $("#begin").click(function () {
            currentPage = 1;
            SelSmall(currentPage);
            $("#pageNo").html(currentPage);
        })
        //上一页
        $("#prev").click(function () {
            currentPage = parseInt(currentPage )-1;
            if (parseInt(currentPage) <1) {
                alert("已经是第一页了")
            } else {
                SelSmall(currentPage);
                $("#pageNo").html(currentPage);
            }
        })
        //下一页
        $("#next").click(function () {
            currentPage = parseInt(currentPage)+1;
            if (parseInt(currentPage) > parseInt($("#totalPages").html())) {
                alert("已经最后一页了");
                return;
            } else {
                SelSmall(currentPage);
                $("#pageNo").html(currentPage);
            }
        })
        //最后一页
        $("#end").click(function () {
            currentPage = parseInt($("#totalPages").html());
            SelSmall(currentPage);
            $("#pageNo").html(currentPage);
        })
        //到第几页
        $("#jump").click(function () {
            currentPage=document.getElementById("jump1").value;
         /*   if (currentPage > $("#totalPages").html() || currentPage < 1) {
                alert("填写有误!");
                $("#inputPage").val("");
                return;
            }*/
            SelSmall(parseInt(currentPage));
            $("#pageNo").html(currentPage);
        })
    })


    //删除
    function delSsms(id,btn) {
        $.getJSON("/doSmall",{"action":"delSsmall","id":id},callback);
        function callback(data) {
            if(data.flag=="true"){
                var option = confirm("确认删除吗？");
                if (option) {
                    $(btn).parent().parent().remove();
                    alert("删除成功！");
                } else {
                    alert("您取消了删除！")
                }
            }else{
                alert("删除失败,不能删除包含商品的小分类！")
            }
        }
    }
</script>

</body>
</html>
